import GbzAudioManager from "./GbzAudioManager";

export default class GbzSdkManager {
    public static _ins: any = null

    static getIns<T>(): T {
        if (this._ins === null) {
            this._ins = new GbzSdkManager()
            this._ins.init()
        }
        return this._ins
    }

    static get ins() {
        return this.getIns<GbzSdkManager>()
    }

    share_title: string = '熬夜打螺丝，过不了第二关，谁来帮帮我呀？'
    private ad_video = null
    private ad_inter = null
    private ad_banner = null
    private ad_custom_row = null
    private ad_customer_col = null
    ad_video_id: string = '41eummgs5o76lmt6n6'
    ad_inter_id: string = '41eummgs5o76lmt6n6'
    ad_banner_id: string = '41eummgs5o76lmt6n6'
    ad_custom_row_id: string = ''
    ad_custom_col_id: string = ''

    platform() {
        let platform = null
        if (cc.sys.platform == cc.sys.WECHAT_GAME) {
            platform = window['wx']
        } else if (cc.sys.platform == cc.sys.BYTEDANCE_GAME) {
            
            platform = window['tt']
        }
        return platform
    }

    init() {
        this.passiveShare()
        this.getRank()
        this.initAudioEnd()
        this.initBanner()
        this.initInter()
        this.initVideo()
        this.initCustomCol()
        this.initCustomRow()
    }

    activeShare(title?: string) {
        const platform = this.platform()
        if (!platform) return
        title = title || this.share_title
        platform.shareAppMessage({ title });
    }

    passiveShare(title?: string) {
        const platform = this.platform()
        if (!platform) return
        platform.showShareMenu({
            success: (res: any) => { },
            fail: (res: any) => { }
        });
        title = title || this.share_title
        platform.onShareAppMessage(() => {
            return { title }
        });
    }

    initAudioEnd() {
        if (typeof window['wx'] === 'undefined') return
        window['wx'].onAudioInterruptionEnd(() => {
            GbzAudioManager.ins.play()
        })
    }

    initBanner() {
        const platform = this.platform()
        if (!platform) return
        if (this.ad_banner_id == '') return
        const size = platform.getSystemInfoSync();
        if (this.ad_banner == null) {
            this.ad_banner = platform.createBannerAd({
                adUnitId: this.ad_banner_id,
                adIntervals: 30,
                style: {
                    height: size.windowHeight - 80,
                    left: 0,
                    top: 500,
                    width: size.windowWidth
                }
            });
            this.ad_banner.onResize((res: any) => {
                this.ad_banner.style.top = size.windowHeight - this.ad_banner.style.realHeight;
                this.ad_banner.style.left = size.windowWidth / 2 - this.ad_banner.style.realWidth / 2;
            });
            this.ad_banner.onError((err: any) => { });
        }
    }

    showBanner(bool: boolean) {
        const platform = this.platform()
        if (!platform) return
        if (this.ad_banner) {
            bool ? this.ad_banner.show() : this.ad_banner.hide();
        }
    }

    initInter() {
        const platform = this.platform()
        if (!platform) return
        if (this.ad_inter_id == '') return
        if (this.ad_inter == null) {
            this.ad_inter = platform.createInterstitialAd({
                adUnitId: this.ad_inter_id
            });
            this.ad_inter.onError((err: any) => { });
        }
    }

    showInter() {
        const platform = this.platform()
        if (!platform) return
        if (this.ad_inter) {
            this.ad_inter.show().catch((err: any) => { });
        }
    }

    initVideo() {
        const platform = this.platform()
        if (!platform) return
        if (this.ad_video_id == '') return
        if (this.ad_video == null) {
            this.ad_video = platform.createRewardedVideoAd({
                adUnitId: this.ad_video_id
            });
            this.ad_video.onError((err: any) => { });
        }
    }

    showVideo(success: any, fail?: any) {
        const platform = this.platform()
        if (!platform) return success && success('模拟成功，激励奖励已发放')
        if (this.ad_video) {
            this.ad_video.offClose();
            this.ad_video.onClose((res: any) => {
                this.ad_video.offClose();
                if (res && res.isEnded || res === undefined) {
                    return success && success('激励奖励已发放')
                } else {
                    return fail && fail('视频播放中断')
                }
            });
            this.ad_video.show().catch(() => {
                this.ad_video.load()
                    .then(() => this.ad_video.show())
                    .catch((err: any) => {
                        console.log('广告展示失败')
                    })
            });
        } else {
            return fail && fail('该功能尚未开放')
        }
    }

    initCustomRow() {
        const platform = this.platform()
        if (!platform) return
        if (this.ad_custom_row_id == '') return
        const size = platform.getSystemInfoSync();
        if (this.ad_custom_row == null) {
            this.ad_custom_row = platform.createCustomAd({
                adUnitId: this.ad_custom_row_id,
                adIntervals: 30,
                style: {
                    width: 320,
                    left: (size.screenWidth - 320) / 2,
                    top: size.screenHeight - 100,
                    fixed: 0
                }
            })
            this.ad_custom_row.onError((err: any) => { });
        }
    }

    showCustomRow(bool: boolean = true) {
        const platform = this.platform()
        if (!platform) return
        if (this.ad_custom_row) bool ? this.ad_custom_row.show() : this.ad_custom_row.hide();
    }

    initCustomCol() {
        const platform = this.platform()
        if (!platform) return
        if (this.ad_custom_col_id == '') return
        const size = platform.getSystemInfoSync();
        if (this.ad_customer_col == null) {
            this.ad_customer_col = platform.createCustomAd({
                adUnitId: this.ad_custom_col_id,
                adIntervals: 30,
                style: {
                    width: size.screenWidth - 20,
                    left: 10,
                    top: (size.screenHeight - 350) / 2,
                    fixed: 0
                }
            })
            this.ad_customer_col.onError((err: any) => { });
        }
    }

    showCustomCol(bool: boolean = true) {
        const platform = this.platform()
        if (!platform) return
        if (this.ad_customer_col) bool ? this.ad_customer_col.show() : this.ad_customer_col.hide();
    }

    getRank() {
        const platform = this.platform()
        if (!platform) return
        platform.postMessage({
            event: 'getRank'
        })
    }

    setRank(data: number) {
        const platform = this.platform()
        if (!platform) return
        platform.postMessage({
            event: 'setRank',
            data: data
        })
    }
}

